package DTO;

import global.global;

import java.util.ArrayList;

import database.DatabaseHelper;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;

public class FineDTO {
	private String ID;
	private String Content;
	private String VehicleName;
	private String Fine;
	private String ExtraFines;
	
	public String getID() { return ID; }
	public void setID(String iD) { ID = iD; }
	
	public String getContent() { return Content; }
	public void setContent(String content) { Content = content; }
	
	public String getVehicleName() { return VehicleName; }
	public void setVehicleName(String vehicleName) { VehicleName = vehicleName; }
	
	public String getFine() { return Fine; }
	public void setFine(String fine) { Fine = fine; }
	
	public String getExtraFines() { return ExtraFines; }
	public void setExtraFines(String extraFines) { ExtraFines = extraFines; }
	
	public static ArrayList<FineDTO> getFineByKeyWord(Context context, String keywords, String vehicleID){
		ArrayList<FineDTO> result = new ArrayList<FineDTO>();
		String query = "";
		if(keywords == null || keywords.isEmpty())
		{
			query = "SELECT Fines.ID, Fines.Content, Vehicles.Name, Fines, ExtraFines " +
					"FROM Fines LEFT JOIN Vehicles ON Fines.Vehicle = Vehicles.ID " +
					"WHERE Fines.Vehicle = '" + vehicleID + "'";
		}
		else
		{
			keywords = global.removeAccent(keywords);
			query = "SELECT Fines.ID, Fines.Content, Vehicles.Name, Fines, ExtraFines " +
					"FROM Fines LEFT JOIN Vehicles ON Fines.Vehicle = Vehicles.ID " +
					"WHERE UnsignContent like '%"+ keywords.trim()+"%' "+
					"and Fines.Vehicle = '" + vehicleID + "'";
		}
		DatabaseHelper myDbHelper = DatabaseHelper.getInstance(context);
	 	try {
	 		myDbHelper.openDatabase();
	 	}catch(SQLException sqle){
	 		throw sqle;
	 	}
	 	///////////////////////////////////////////
	 	Cursor cursor = myDbHelper.getMyDataBase().rawQuery(query, null);
		if(cursor.moveToFirst()){
			do{
				FineDTO resultItem = new FineDTO();

				//Set properties
				resultItem.setID(cursor.getString(cursor.getColumnIndex("ID")));
				resultItem.setContent(cursor.getString(cursor.getColumnIndex("Content")));
				resultItem.setVehicleName(cursor.getString(cursor.getColumnIndex("Name")));
				resultItem.setFine(cursor.getString(cursor.getColumnIndex("Fines")));			
	 			try{
	 				resultItem.setExtraFines(cursor.getString(cursor.getColumnIndex("ExtraFines")));
	 			}catch(Exception e){
	 				resultItem.setExtraFines("");
	 			}
	 			
	 			result.add(resultItem);
			}while(cursor.moveToNext());
		}
	 	myDbHelper.close();
	 	return result;
	}
}
